//
//  ServiceAccessHelper.m
//  ShowTimeApp
//
//  Created by Ruby on 10/29/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ServiceAccessHelper.h"
#import "CinemaService.h"

@implementation ServiceAccessHelper
NSString* const CinemaTableName = @"CINEMA";
NSString* const MovieTableName =@"MOVIE";
NSString* const ShowtimeTableName =@"SHOWTIME";
NSString* const TicketInfoTableName =@"TICKETINFO";
@synthesize binding;

-(id)initWithServiceLink:(NSString *)link{
    [self init];
    self.binding=[[CinemaService CinemaServiceSoapBinding]initWithAddress:link];
    self.binding.logXMLInOut=YES;
    return self;
}

-(void)getCinemaListAsync{
    tableName=CinemaTableName;
    CinemaService_GetAllCinemas *parms = [[CinemaService_GetAllCinemas alloc]init];
    [self.binding GetAllCinemasAsyncUsingParameters:parms delegate:self];
}

-(void)getMovieListAsync{
    tableName=MovieTableName;
    CinemaService_GetAllMoviesInCurrentDayToNext7DaysOfShowTime *parms = [[CinemaService_GetAllMoviesInCurrentDayToNext7DaysOfShowTime alloc]init];
    [self.binding GetAllMoviesInCurrentDayToNext7DaysOfShowTimeAsyncUsingParameters:parms delegate:self];
}

-(void)getShowTimeListAsync{
    tableName=ShowtimeTableName;
    CinemaService_GetAllShowTimeInCurrentDayToNext7Days *parms = [[CinemaService_GetAllShowTimeInCurrentDayToNext7Days alloc]init];
    [self.binding GetAllShowTimeInCurrentDayToNext7DaysAsyncUsingParameters:parms delegate:self];
}

-(void)getTicketInfoListAsync{
    tableName=TicketInfoTableName;
    CinemaService_GetAllTicketInfo *parms = [[CinemaService_GetAllTicketInfo alloc]init];
    [self.binding GetAllTicketInfoAsyncUsingParameters:parms delegate:self];
}

-(void)operation:(CinemaServiceSoapBindingOperation *)operation completedWithResponse:(CinemaServiceSoapBindingResponse *)response{
    [NSThread sleepForTimeInterval:0.5];
    if(tableName==CinemaTableName){
        [self getCinemaListComplete:response];
    }else if(tableName==MovieTableName){
        [self getCinemaListComplete:response];
    }else if(tableName==ShowtimeTableName){
        [self getCinemaListComplete:response];
    }else{
        [self getCinemaListComplete:response];
    }
}

-(NSMutableArray*)getCinemaListComplete:(CinemaServiceSoapBindingResponse *)response{
    for(id mine in response.bodyParts){
        if(tableName==CinemaTableName){
        CinemaService_ArrayOfCinemaEntity* cinemaList = [mine GetAllCinemasResult];
        return cinemaList.CinemaEntity;
        }
    }
    return nil;
}
-(NSMutableArray*)getMovieListComplete:(CinemaServiceSoapBindingResponse *)response{
    for(id mine in response.bodyParts){
        if(tableName==MovieTableName){
            CinemaService_ArrayOfMovieEntity* movieList = [mine GetAllMoviesInCurrentDayToNext7DaysOfShowTimeResult];
            return movieList.MovieEntity;
        }
    }
    return nil;
}

-(NSMutableArray*)getShowTimeListComplete:(CinemaServiceSoapBindingResponse *)response{
        for(id mine in response.bodyParts){
            if(tableName==ShowtimeTableName){
                CinemaService_ArrayOfShowTimeEntity* showtimeList = [mine GetAllShowTimeInCurrentDayToNext7DaysResult];
                return showtimeList.ShowTimeEntity;
            }
        }
    return nil;
}

-(NSMutableArray*)getTicketInfoListComplete:(CinemaServiceSoapBindingResponse *)response{
    for(id mine in response.bodyParts){
        if(tableName==TicketInfoTableName){
            CinemaService_ArrayOfTicketInfoEntity* ticketInfoList = [mine GetAllTicketInfoResult];
            return ticketInfoList.TicketInfoEntity;
        }
    }
    return nil;
}
@end
